package com.lanou.myapp.book.boybook.contentboybook;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.GsonRequest;
import com.lanou.myapp.imageloader.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/19.
 */
public class ContentBoyBookActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ImageView imageView;
    private TextView tv_name, tv_author, tv_type, tv_wordNum, tv_evaluate, tv_tag1, tv_tag2, tv_tag3, tv_tag4;
    private TextView tv_description;
    private ImageView iv_readerImg, iv_readerImg2;
    private TextView tv_readerName, tv_readerName2;
    private TextView tv_readerSay, tv_readerSay2;
    private TextView tv_from, tv_from2, tv_lv, tv_lv2;
    private BoyBookAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_boy_book_activity);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView_content_boy_book);
//        todayRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        adapter = new BoyBookAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));


        imageView = (ImageView) findViewById(R.id.iv_content_name_boy_book);
        iv_readerImg = (ImageView) findViewById(R.id.iv_content_readerImg_boy_book);
        iv_readerImg2 = (ImageView) findViewById(R.id.iv_content_readerImg2_boy_book);
        tv_name = (TextView) findViewById(R.id.tv_content_name_boy_book);
        tv_author = (TextView) findViewById(R.id.tv_content_author_boy_book);
        tv_type = (TextView) findViewById(R.id.tv_content_type_boy_book);
        tv_wordNum = (TextView) findViewById(R.id.tv_content_wordsCnt_boy_book);
        tv_evaluate = (TextView) findViewById(R.id.tv_content_evaluate_boy_book);
        tv_tag1 = (TextView) findViewById(R.id.tv_content_tag1_boy_book);
        tv_tag2 = (TextView) findViewById(R.id.tv_content_tag2_boy_book);
        tv_tag3 = (TextView) findViewById(R.id.tv_content_tag3_boy_book);
        tv_tag4 = (TextView) findViewById(R.id.tv_content_tag4_boy_book);
        tv_description = (TextView) findViewById(R.id.tv_content_description_boy_book);
        tv_readerName = (TextView) findViewById(R.id.tv_content_readerName_boy_book);
        tv_readerName2 = (TextView) findViewById(R.id.tv_content_readerName2_boy_book);
        tv_readerSay = (TextView) findViewById(R.id.tv_content_comment_boy_book);
        tv_readerSay2 = (TextView) findViewById(R.id.tv_content_comment2_boy_book);
        tv_from = (TextView) findViewById(R.id.tv_content_from_boy_book);
        tv_from2 = (TextView) findViewById(R.id.tv_content_from2_boy_book);
        tv_lv = (TextView) findViewById(R.id.tv_lv);
        tv_lv2 = (TextView) findViewById(R.id.tv_lv);

        Intent intent = getIntent();
        int a = intent.getIntExtra("key", 0);
        String b = "http://4g.if.qidian.com/Atom.axd/Api/Book/Get?bookId=";
        String c = "&preview=0";
        String url = b + a + c;
        Log.d("dfdfadsfg", url);


        GsonRequest<ContentBoyBookBeans> request = new GsonRequest<ContentBoyBookBeans>(url, ContentBoyBookBeans.class, new Response.Listener<ContentBoyBookBeans>() {
            @Override
            public void onResponse(ContentBoyBookBeans response) {
                Log.d("xxxxxxx", response.getData().getBookName());
                tv_name.setText(response.getData().getBookName());
                tv_author.setText(response.getData().getCategoryName());
                tv_wordNum.setText(String.valueOf(response.getData().getWordsCnt() / 10000));
                tv_evaluate.setText(String.valueOf(response.getData().getBssReadTotal() / 10000));
                tv_tag1.setText(response.getData().getMonthTicketCount() + "月票");
                tv_tag2.setText(response.getData().getVoteAll() / 1000 + "+推荐");
                tv_tag3.setText(response.getData().getDonateCount() + "打赏");
                tv_tag4.setText(response.getData().getBookForumCount() + "粉丝");
                tv_description.setText(response.getData().getDescription());
                tv_readerName.setText(response.getData().getBookReviewList().get(0).getUserName());
                tv_readerSay.setText(response.getData().getBookReviewList().get(0).getBody());
                tv_readerName2.setText(response.getData().getBookReviewList().get(1).getUserName());
                tv_readerSay2.setText(response.getData().getBookReviewList().get(1).getBody());
                tv_lv.setText(response.getData().getBookReviewList().get(0).getRankName());
                tv_lv2.setText(response.getData().getBookReviewList().get(1).getRankName());
                tv_from.setText(response.getData().getBookReviewList().get(0).getFrom());
                tv_from2.setText(response.getData().getBookReviewList().get(1).getFrom());

                ImageLoader.ImageListener listener2 = ImageLoader.getImageListener(iv_readerImg, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
                VolleySingleton.getImageLoader().get(response.getData().getBookReviewList().get(0).getUserHeadIcon(), listener2,400,300);

                ImageLoader.ImageListener listener3 = ImageLoader.getImageListener(iv_readerImg2, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
                VolleySingleton.getImageLoader().get(response.getData().getBookReviewList().get(1).getUserHeadIcon(), listener3,400,300);

                adapter.addData(response.getData().getSameRecommend());
                recyclerView.setAdapter(adapter);


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("aaa", "error");
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("Host","4g.if.qidian.com");
                map.put("Cookie", "QDInfo=R/voDN8t5IqnUh6Fa/DucQebL2wPkFimgJfQlfkgHbqYIhf8YwdUS0XzWB6/qdT8115Nk3e3kH+ma7m84lNOc7sJpP3//Jd7++k/UZlAMw6EkTZIUfGAE9/hJhRkSUphhOGOouDPUL+UE0Rn69xni27F08BqyR7j; domain=.qidian.com");
                return map;
            }
        };

        VolleySingleton.getInstance()._addRequest(request);

    }

}
